﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using CardInChina.Data.Entity.Entity;

namespace CardInChina.Web.Mvc
{
    public static class CustomHtmlHelper
    {
        //每页数
        public const int PageCount = 5;
        /// <summary>
        /// order form
        /// </summary>
        /// <param name="html"></param>
        /// <param name="cardModel"></param>
        /// <returns></returns>
        public static MvcHtmlString OrderForm(this HtmlHelper html, Card cardModel)
        {
            StringBuilder result = new StringBuilder(string.Empty);
            if (cardModel.GoodsType == 1)
            {
                if (!string.IsNullOrEmpty(cardModel.AccountName))
                    BuildAccountName(result, cardModel);
                if (!string.IsNullOrEmpty(cardModel.ChargeMethod))
                    BuildChargeMethod(result, cardModel);
                if (!string.IsNullOrEmpty(cardModel.ServerArea))
                    BuildServerArea(result, cardModel);
                if (!string.IsNullOrEmpty(cardModel.ServerNames))
                    BuildServerNames(result, cardModel);
            }
            return MvcHtmlString.Create(result.ToString());
        }

        private static void BuildAccountName(StringBuilder src, Card cardModel)
        {
            BuildItemContainerStart(src);
            src.Append("<input type='hidden' name='gameid_name' value='");
            src.Append(cardModel.AccountName);
            src.Append("'>");
            src.Append(cardModel.AccountName);
            src.Append("<input type=\"text\" name='GameID' id='gameid'  class=\"text1\"/>");
            BuildItemContainerEnd(src);
        }

        private static void BuildChargeMethod(StringBuilder src, Card cardModel)
        {
            BuildItemContainerStart(src);
            src.Append("<input type='hidden' name='gamename_name' value='");
            src.Append(cardModel.ChargeMethod);
            src.Append("'>");
            src.Append(cardModel.ChargeMethod);
            var options = cardModel.OfChargeOptions;
            if (options != null)
            {
                var options2 = options.Where(o => o.OptionNumber == 1 && o.Status == 1);
                if (options2.Count() > 0)
                {
                    src.Append("<select name='GameName' id='gamename'><option value=''>--请选择--</option>");

                    foreach (ChargeOption option in options2)
                    {
                        src.Append("<option value='");
                        src.Append(option.ChargeOptionID);
                        src.Append("'>");
                        src.Append(option.OptionName);
                        src.Append("</option>");
                    }
                    src.Append("</select>");
                    src.Append("<input name='GameSelected' type='hidden' value='1' />");
                }
                else
                {
                    src.Append("<input type=\"type\" name='GameName' id='gamename'>");
                }
            }
            else
            {
                src.Append("<input type=\"type\" name='gamename' id='gamename'>");

            }
            BuildItemContainerEnd(src);
        }

        private static void BuildServerArea(StringBuilder src, Card cardModel)
        {
            BuildItemContainerStart(src);
            src.Append("<input type='hidden' name='server_area_name' value='");
            src.Append(cardModel.ServerArea);
            src.Append("'>");
            src.Append(cardModel.ServerArea);

            var options = cardModel.OfChargeOptions;
            if (options != null)
            {
                var options2 = options.Where(o => o.OptionNumber == 2 && o.Status == 1);
                if (options2.Count() > 0)
                {
                    src.Append("<select name='ServerArea' id='server_area'><option value=''>--请选择--</option>");

                    foreach (ChargeOption option in options2)
                    {
                        src.Append("<option value='");
                        src.Append(option.OptionName);
                        src.Append("'>");
                        src.Append(option.OptionName);
                        src.Append("</option>");
                    }
                    src.Append("</select>");
                }
                else
                {
                    src.Append("<input type=\"type\" name='ServerArea' id='server_area'>");
                }
            }
            else
            {
                src.Append("<input type=\"type\" name='ServerArea' id='server_area'>");

            }

            BuildItemContainerEnd(src);
        }

        private static void BuildServerNames(StringBuilder src, Card cardModel)
        {
            BuildItemContainerStart(src);
            src.Append("<input type='hidden' name='servername_name' value='");
            src.Append(cardModel.ServerNames);
            src.Append("'>");
            src.Append(cardModel.ServerNames);

            var options = cardModel.OfChargeOptions;
            if (options != null)
            {
                var options2 = options.Where(o => o.OptionNumber == 3 && o.Status == 1);
                if (options2.Count() > 0)
                {
                    src.Append("<select name='ServerName' id='servername'><option value=''>--请选择--</option>");

                    foreach (ChargeOption option in options2)
                    {
                        src.Append("<option value='");
                        src.Append(option.OptionName);
                        src.Append("'>");
                        src.Append(option.OptionName);
                        src.Append("</option>");
                    }
                    src.Append("</select>");
                }
                else
                {
                    src.Append("<input type=\"type\" name='ServerName' id='servername'>");
                }
            }
            else
            {
                src.Append("<input type=\"type\" name='ServerName' id='servername'>");

            }

            BuildItemContainerEnd(src);

        }

        private static void BuildItemContainerStart(StringBuilder src)
        {
            src.Append("<div class=\"item\">");
        }

        private static void BuildItemContainerEnd(StringBuilder src)
        {
            src.Append("</div>");
        }


        /// <summary>
        /// order list tab
        /// </summary>
        /// <param name="html"></param>
        /// <param name="ordertype"></param>
        /// <returns></returns>
        public static MvcHtmlString OrderListTab(this HtmlHelper html, string ordertype)
        {
            StringBuilder tagBuilder = new StringBuilder();
            switch (ordertype)
            {
                case "tobepaid":
                    tagBuilder.Append("<li class='c'>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "未支付", "Order", "Customer", new { ordertype = "tobepaid" }, null).ToString());
                    tagBuilder.Append("</li>");
                    tagBuilder.Append("<li>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "已支付", "Order", "Customer", new { ordertype = "paid" }, null).ToString());
                    tagBuilder.Append("</li>");
                    tagBuilder.Append("<li>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "已取消", "Order", "Customer", new { ordertype = "cancelled" }, null).ToString());
                    tagBuilder.Append("</li>");
                    break;
                case "paid":
                    tagBuilder.Append("<li>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "未支付", "Order", "Customer", new { ordertype = "tobepaid" }, null).ToString());
                    tagBuilder.Append("</li>");
                    tagBuilder.Append("<li  class='c'>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "已支付", "Order", "Customer", new { ordertype = "paid" }, null).ToString());
                    tagBuilder.Append("</li>");
                    tagBuilder.Append("<li>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "已取消", "Order", "Customer", new { ordertype = "cancelled" }, null).ToString());
                    tagBuilder.Append("</li>");
                    break;
                case "cancelled":
                    tagBuilder.Append("<li>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "未支付", "Order", "Customer", new { ordertype = "tobepaid" }, null).ToString());
                    tagBuilder.Append("</li>");
                    tagBuilder.Append("<li>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "已支付", "Order", "Customer", new { ordertype = "paid" }, null).ToString());
                    tagBuilder.Append("</li>");
                    tagBuilder.Append("<li class='c'>");
                    tagBuilder.Append(System.Web.Mvc.Html.LinkExtensions.ActionLink(html, "已取消", "Order", "Customer", new { ordertype = "cancelled" }, null).ToString());
                    tagBuilder.Append("</li>");
                    break;
                default: break;
            }
            return MvcHtmlString.Create(tagBuilder.ToString());
        }

        /// <summary>
        /// custome paging
        /// </summary>
        /// <param name="html"></param>
        /// <param name="totalCount"></param>
        /// <param name="pageCount"></param>
        /// <param name="currentPage"></param>
        /// <param name="pagelink"></param>
        /// <returns></returns>
        public static MvcHtmlString CustomPaging(this HtmlHelper html, int totalCount, int pageCount, int currentPage,
            Func<int, string> pagelink)
        {
            if (totalCount < 0) throw new Exception("totalCount must positive integer!");
            if (pageCount < 0) throw new Exception("pageCount must positive integer!");
            if (currentPage < 1) throw new Exception("currentPage must positive integer!");
            if (currentPage * pageCount >= totalCount + pageCount) throw new Exception("invalid currentPage!");
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"page-bottom\">");


            //上一页
            //disabled
            if (currentPage == 1)
            {
                sb.Append("<a class=\"page-start-no\">上一页</a>");
            }
            else
            {
                sb.Append("<a class=\"page-start ui-icons\" href=\"");
                sb.Append(pagelink(currentPage - 1));
                sb.Append("\">上一页</a>");
            }

            //中间页
            int i = 1;
            bool prev = false;
            bool next = false;
            for (i = 1; i * pageCount < totalCount + pageCount; i++)
            {
                if (i == currentPage)
                {
                    sb.Append("<a class=\"page-cur ui-icons\" href=\"");
                    sb.Append(pagelink(i));
                    sb.Append("\">");
                    sb.Append(i);
                    sb.Append("</a>");
                }
                else if (i == 1 || (i >= currentPage - 2 && i <= currentPage + 2) || i * pageCount >= totalCount)
                {
                    sb.Append("<a class=\"page-next ui-icons\" href=\"");
                    sb.Append(pagelink(i));
                    sb.Append("\">");
                    sb.Append(i);
                    sb.Append("</a>");
                }
                else if (i < currentPage - 2 && prev == false)
                {
                    sb.Append("<span>...</span>");
                    prev = true;
                }
                else if (i > currentPage + 2 && next == false)
                {
                    sb.Append("<span>...</span>");
                    next = true;
                }

            }

            //下一页，i-1为总共的页数
            //disabled
            if (currentPage == i - 1)
            {
                sb.Append("<a class=\"page-end-no\">下一页</a>");
            }
            else
            {
                sb.Append("<a class=\"page-end ui-icons\" href=\"");
                sb.Append(pagelink(currentPage + 1));
                sb.Append("\">下一页</a>");
            }
            //跳转页
            //sb.Append("<span class=\"page-skip\">共");
            //sb.Append(i-1);
            //sb.Append("页 到第");
            //sb.Append("<input id=\"pageGoto\" type=\"text\" value=\"\" class=\"jumpto ui-icons\" />页 </span>");
            //sb.Append("<a id=\"page_goto\" href=\"#\" class=\"page-Go ui-icons\">Go</a>");
            sb.Append("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }


        public static MvcHtmlString SiteUrl(this HtmlHelper html, string content)
        {
            StringBuilder result = new StringBuilder();
            if (content != null && !content.StartsWith("/"))
                content = "/" + content;
            result.AppendFormat("http://{0}{1}", HttpContext.Current.Request.Url.Authority, content);
            return MvcHtmlString.Create(result.ToString());
        }
    }
}